<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Homeold extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
    public function getUserAuth($user_id)
    {
        $userInfo = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->field('user_auth')
            ->find();
        $userAuth = [];
        if($userInfo['user_auth']){
            $userAuth = explode(',',trim($userInfo['user_auth'],','));
        }
        return $userAuth;
    }
    /**
     * 获取about表数据：
     * type：1 注册协议    2 隐私协议    3 关于我们    4 我的页面的背景图
     * @return string
     */
    public function getAbout()
    {
        extract(input());
        $res = Db::table('up_about')
            ->where('id',$type)
            ->find();
		$phone = '';
		if($type == 5){
			$phone = Db::table('up_about')
				->where('id',4)
				->find();
		}
        return json(['status'=>200,'msg'=>$res,'phone'=>$phone]);
    }
    /**
     * location 自动定位返回id：location
     * $location  地区名称
     */
    public function locationGet (){
        extract(input());
        $res = array();
        if(isset($sheng)){
            $shengID = Db::table('up_area')
                ->where('district',$sheng)
                ->where('level',1)
                ->value('district_id');
            $res['sheng'] = $shengID;
        }
        if(isset($shi)){
            $shiID = Db::table('up_area')
                ->where('district',$shi)
                ->where('level',2)
                ->value('district_id');
            $res['shi'] = $shiID;
        }
        if(isset($qu)){
            $quID = Db::table('up_area')
                ->where('district',$qu)
                ->where('level',3)
                ->value('district_id');
            $res['qu'] = $quID;
        }
        return json(['status'=>200,'msg'=>$res]);
    }
    /**
     * 获取首页轮播图
     */
    public function getLunbo(){
        extract(input());
        //1、轮播
        $lunbo = Db::table('up_banner')
            ->where('banner_status',1)
            ->order('banner_sort desc')
            ->field('banner_id,banner_type,banner_img,banner_title')
            ->select();
        return json(['status'=>200,'msg'=>$lunbo]);
    }
    /**
     * 获取首页数据
     * @return \think\response\Json
     */
    public function getHomeData()
    {
        extract(input());
        if(isset($ups) && isset($downs)){
            $ups = explode(',',$ups);
            $downs = explode(',',$downs);
            foreach ($ups as $k => $v){
                ${"$v"} = $downs[$k]; //赋值
            }
        }
        $types = $this->getAuthTypes($user_id,1);
        $userAuth = $this->getUserAuth($user_id);
        $where = [];
        if(isset($city) && $city){
            $where['s_p_id'] = $city;
        }
        if(isset($qu) && $qu){
            $where['s_qu'] = $qu;
        }
        //-----------------------pp_id start -----------------------
        $pb_where = [];
        $pb_where2 = [];
        $where_in = '';
        if(isset($menDian) && $menDian){
            $pb_where = [
                'pb_where' => 2
            ];
            if($menDian == 1){
                $where_in = 'in';
            }else{
                $where_in = 'not in';
            }
        }
        if(isset($zhaoPin) && $zhaoPin){
            $pb_where = [
                'pb_where' => 8
            ];
            if($zhaoPin == 1){
                $where_in = 'in';
            }else{
                $where_in = 'not in';
            }
        }
        if(isset($menDian) && $menDian && isset($zhaoPin) && $zhaoPin){
            if($menDian == 1 && $zhaoPin == 1){
                $pb_where = [
                    'pb_where' => 2
                ];
                $pb_where2 = [
                    'pb_where' => 8
                ];
                $where_in = 'in';
            }
            if($menDian == 2 && $zhaoPin == 2){
                $pb_where = [
                    'pb_where' => ['in','2,8']
                ];
                $where_in = 'not in';
            }
            if($menDian == 1 && $zhaoPin == 2){
                $pb_where = [
                    'pb_where' => 2
                ];
                $pb_where2 = [
                    'pb_where' => 8
                ];
                $where_in = 'not in';
            }
            if($menDian == 2 && $zhaoPin == 1){
                $pb_where = [
                    'pb_where' => 8
                ];
                $pb_where2 = [
                    'pb_where' => 2
                ];
                $where_in = 'not in';
            }
        }
        if($pb_where){
            $ppinds = Db::table('up_pp_banner')
                ->where($pb_where)
                ->group('pb_pp_id')
                ->column('pb_pp_id');
            if($pb_where2){
                $ppinds2 = Db::table('up_pp_banner')
                    ->where('pb_pp_id','in',$ppinds)
                    ->where($pb_where2)
                    ->group('pb_pp_id')
                    ->column('pb_pp_id');
                $ppinds = $ppinds2;
            }
            $where['pp_id'] = [$where_in,$ppinds];
        }
        //----------------------pp_id end --------------------------
        //----------------------------------------------------------
        for($i=1;$i<=10;$i++){
            if(isset(${'ten' . $i}) && ${'ten' . $i}){
                $where['ten' . $i] = ${'ten' . $i};
            }
        }
        //----------------------------------------------------------
        if($where){
            if($userAuth){
                $tids_home = array_column($types,'t_id');
                $where['pp_t_id'] = ['in',$tids_home];
            }
            $data = Db::table('up_store')->alias('s')
                ->join('up_pp pp','s.s_pp_id=pp.pp_id','LEFT')
                ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                ->where($where)
                ->group('t_id')
                ->order('t_sort desc')
                ->field('t_id,t_name,t_thumb,t_remark')
                ->select();
            //--------分类跟着内容定-start----------------------
            $types = Db::table('up_store')->alias('s')
                ->join('up_pp pp','s.s_pp_id=pp.pp_id','LEFT')
                ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                ->where($where)
                ->group('t_id')
                ->order('t_sort desc')
                ->page(1,7)
                ->field('t_id,t_name,t_thumb,t_remark')
                ->select();
            //--------分类跟着内容定- end ----------------------
            if($data){
                if($userAuth){
                    $ppids = $this->getAuthTypes($user_id,2);
                    $where['pp_id'] = ['in',$ppids];
                }
                foreach ($data as $k => $v){
                    $where['pp_t_id'] = $v['t_id'];
                    $son = Db::table('up_store')->alias('s')
                        ->join('up_pp pp','s.s_pp_id=pp.pp_id','LEFT')
                        ->where($where)
                        ->group('s_pp_id')
                        ->order('pp_sort desc')
                        ->field('pp_id,pp_name,pp_thumb')
                        ->select();
                    if($son){
                        $data[$k]['son'] = $son;
                    }else{
                        unset($data[$k]); 
                    }

                }
            }
        }else{
            $data = $this->getAuthTypes($user_id);
            if($data){
                $where = [];
                if($userAuth){
                    $ppids = $this->getAuthTypes($user_id,2);
                    $where['pp_id'] = ['in',$ppids];
                }
                foreach ($data as $k => $v){
                    $where['pp_t_id'] = $v['t_id'];
                    $son = Db::table('up_pp')
                        ->where($where)
                        ->order('pp_sort desc')
                        ->field('pp_id,pp_name,pp_thumb')
                        ->select();
                    if($son){
                        $data[$k]['son'] = $son;
                    }else{
                        unset($data[$k]);
                    }
                }
            }
        }
        return json(['status'=>200,'msg'=>$types,'data'=>$data]);
    }

    /**
     * 获取所有的筛选条件
     */
    public function getAllOptions(){
        extract(input());
        $userAuth = $this->getUserAuth($user_id);
        $data = [];
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id id,district name,keykey')
            ->select();
        $places = Db::table('up_place')
            ->order('p_sort desc')
            ->field('p_id id,p_name name,p_key keykey')
            ->select();
//        1、城市区域，2、家居商场，3、品牌优惠，4、一大品牌，5、二大品牌、
//        6、三大品牌、7、四大品牌、8、五大品牌，9、六大品牌，10、七大品牌，
//        11、八大品牌，12、九大品牌，13、十大品牌，14、人才招聘
        //1、城市区域，2、家居商场
        $data['城市区域'] = $qus;
        $data['家居商场'] = $places;
        //3、品牌优惠
        //ppBanner : 2店面优惠
        $menDian = Db::table('up_pp_banner')
            ->where('pb_where',2)
            ->find();
        if($menDian && !$userAuth){
            $data['品牌优惠'] = [['id' => 1,'name' => '有优惠','keykey' => 'menDian'],['id' => 2,'name' => '无优惠','keykey' => 'menDian']];
        }
        //十大自定义
        $tens = Db::table('up_ten')
            ->where('ten_status',1)
            ->order('ten_sort desc')
            ->field('ten_id id,ten_name name,ten_key keykey')
            ->select();
        if($tens && !$userAuth){
            foreach ($tens as $k => $v){
                $data[$v['name']] = [['id' => 1,'name' => '是','keykey' => $v['keykey']],['id' => 2,'name' => '否','keykey' => $v['keykey']]];
            }
        }
        //  14、人才招聘
        //ppBanner : 8人才招聘
        $zhaoPin = Db::table('up_pp_banner')
            ->where('pb_where',8)
            ->find();
        if($zhaoPin && !$userAuth){
            $data['人才招聘'] = [['id' => 1,'name' => '有招聘','keykey' => 'zhaoPin'],['id' => 2,'name' => '无招聘','keykey' => 'zhaoPin']];
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 获取全部南京内的区
     */
    public function getQus(){
        extract(input());
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id,district')
            ->select();
        return json(['status'=>200,'msg'=>$qus]);
    }
    /**
     * 获取全部装饰城
     */
    public function getPlaces(){
        extract(input());
        $types = Db::table('up_place')
            ->order('p_sort desc')
            ->select();
        return json(['status'=>200,'msg'=>$types]);
    }
    /**
     * 获取全部分类
     */
    public function getTypes(){
        extract(input());
        $types = $this->getAuthTypes($user_id);
        return json(['status'=>200,'msg'=>$types]);
    }
    /**
     * 获取全部分类
     */
    public function getAuthTypes($user_id,$type=''){
        $limit = 500;
        if($type == 1) $limit = 7;
        $userAuth = $this->getUserAuth($user_id);
        if(!$userAuth){
            $types = Db::table('up_types')
                ->order('t_sort desc')
                ->page(1,$limit)
                ->select();
        }else{
            $two = [];
            $ppids = [];
            if(in_array(11,$userAuth)) $two[] = 2;
            if(in_array(12,$userAuth)) $two[] = 8;
            if($two){
                $ppids = Db::table('up_pp_banner')
                    ->where('pb_where','in',$two)
                    ->group('pb_pp_id')
                    ->column('pb_pp_id');
            }
            $str = '';
            foreach ($userAuth as $k => $v){
                if($v <= 10){
                    $str .= 'ten' . $v . '|';
                }
            }
            if($str){
                $str = trim($str,'|');
                if($type == 2){
                    $back_ppids = Db::table('up_pp')
                        ->where('pp_id','in',$ppids)
                        ->whereOr($str,1)
                        ->column('pp_id');
                    return $back_ppids;
                }
                $tids = Db::table('up_pp')
                    ->where('pp_id','in',$ppids)
                    ->whereOr($str,1)
                    ->group('pp_t_id')
                    ->column('pp_t_id');
            }else{
                if($type == 2){
                    return $ppids;
                }
                $tids = Db::table('up_pp')
                    ->where('pp_id','in',$ppids)
                    ->group('pp_t_id')
                    ->column('pp_t_id');
            }
            $types = Db::table('up_types')
                ->where('t_id','in',$tids)
                ->order('t_sort desc')
                ->page(1,$limit)
                ->select();
        }

        return $types;
    }
    /**
     * 搜索
     * keyword：关键字
     */
    public function searchHome(){
        extract(input());
        $userAuth = $this->getUserAuth($user_id);
        $where = [
            'pp_name' => ['like',"%{$keyword}%"]
        ];
        if($userAuth){
            $ppids = $this->getAuthTypes($user_id,2);
            $where['pp_id'] = ['in',$ppids];
        }
        $datas = Db::table('up_pp')
            ->where($where)
            ->order('pp_name asc')
            ->page($page,20)
            ->select();
        return json(['status'=>200,'msg'=>$datas]);
    }
    /**
     * 获取某分类下的所有品牌
     * t_id：分类名字
     */
    public function getTypePps(){
        extract(input());
        $userAuth = $this->getUserAuth($user_id);
        $where = [
            'pp_t_id' => $t_id
        ];
        if($userAuth){
            $ppids = $this->getAuthTypes($user_id,2);
            $where['pp_id'] = ['in',$ppids];
        }
        $datas = Db::table('up_pp')
            ->where($where)
            ->order('pp_sort desc')
            ->select();
        return json(['status'=>200,'msg'=>$datas]);
    }

    /**
     * 品牌详情：ppInfo
     * pp_id
     */
    public function ppInfo(){
        extract(input());
        $data = Db::table('up_pp')
            ->where('pp_id',$pp_id)
            ->find();
        if($data['pp_images']){
            $data['pp_images'] = explode(',',$data['pp_images']);
        }
        $like = Db::table('up_like')
            ->where('like_user_id',$user_id)
            ->where('like_pp_id',$pp_id)
            ->find();
        $data['like'] = 1; //我没有赞
        if($like){
            $data['like'] = 2;
        }
        //1、轮播数据
        $lunbo = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',1)
            ->order('pb_sort desc')
            ->select();
        //2、门店数据
        $store = Db::table('up_store')
            ->where('s_pp_id',$pp_id)
            ->order('s_sort desc')
            ->select();
        //---------入浏览记录-------
        $today = date('Y-m-d H:i:s');
        $have = Db::table('up_history')
            ->where('h_user_id',$user_id)
            ->where('h_pp_id',$pp_id)
            ->find();
        if($have){
            Db::table('up_history')->where('h_id',$have['h_id'])->setField('h_time',$today);
        }else{
            Db::table('up_history')->insert([
                'h_pp_id' => $pp_id,
                'h_user_id' => $user_id,
                'h_time' => $today
            ]);
        }
        //--------------------------------
        //1、相册（2店面优惠、3品牌介绍、4服务范围、5产品图册、6实景案例、7精英团队、8人才招聘）
        $you_hui = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',2)
            ->order('pb_sort desc')
            ->select();
        $jie_shao = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',3)
            ->order('pb_sort desc')
            ->select();
        $fu_wu = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',4)
            ->order('pb_sort desc')
            ->select();
        $xiang_ce = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',5)
            ->order('pb_sort desc')
            ->select();
        $an_li = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',6)
            ->order('pb_sort desc')
            ->select();
        $tuan_dui = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',7)
            ->order('pb_sort desc')
            ->select();
        $zhao_pin = Db::table('up_pp_banner')
            ->where('pb_pp_id',$pp_id)
            ->where('pb_where',8)
            ->order('pb_sort desc')
            ->select();
        //企业资料
        $zi_liao = Db::table('up_pp_file')
            ->where('pb_pp_id',$pp_id)
            ->order('pb_sort desc')
            ->select();

        return json([
            'status'=>200,'msg'=>$data,'lunbo'=>$lunbo,'store'=>$store,
            'you_hui'=>$you_hui,        //店面优惠
            'jie_shao'=>$jie_shao,      //品牌介绍
            'fu_wu'=>$fu_wu,            //服务范围
            'xiang_ce'=>$xiang_ce,      //产品图册
            'an_li'=>$an_li,            //实景案例
            'tuan_dui'=>$tuan_dui,      //精英团队
            'zhao_pin'=>$zhao_pin,      //人才招聘
            'zi_liao'=>$zi_liao,      //人才招聘
        ]);
    }

    /**
     * 收藏
     * pp_id：品牌id
     *
     */
    public function likePp(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $like = Db::table('up_like')
            ->where('like_user_id',$user_id)
            ->where('like_pp_id',$pp_id)
            ->find();
        if($like){
           $res = Db::table('up_like')->where('like_id',$like['like_id'])->delete();
        }else{
            $res = Db::table('up_like')->insert([
                'like_pp_id' => $pp_id,
                'like_user_id' => $user_id,
                'like_time' => $today
            ]);
        }
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试！']);
        }
    }
    /**
     * 生成省市区json数据：makeJson
     * @return string
     */
    public function makeJson()
    {
//        set_time_limit(0);
//        $area = file_get_contents('static/area.json');
//        $area2 = json_decode($area,true);
//        return json($area2);
        set_time_limit(0);
        $pro = Db::table('up_area')
            ->where('pid',0)
//            ->where('district_id',1001)
            ->field('district_id id,district name')
            ->order('sort')
            ->select();
        foreach($pro as $k=>$v){
            $pro[$k]['parentId'] = 0;
            $city = Db::table('up_area')
                ->where('pid',$v['id'])
                ->where('level',2)
                ->field('district_id id,district name,pid parentId')
                ->select();
//            foreach ($city as $kk=>$vv){
//                $city[$kk]['sub'] = Db::table('up_area')
//                    ->where('pid',$vv['id'])
//                    ->where('level',3)
//                    ->field('district_id id,district name,pid parentId')
//                    ->select();
//            }
            $pro[$k]['sub'] = $city;
        }
        return json($pro);
    }
}